package net.maixiami;

import com.sleepycat.je.DatabaseException;
import com.sleepycat.persist.EntityCursor;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.domain.Item;
import com.taobao.api.domain.ItemCat;
import com.taobao.api.domain.ShopCat;
import com.taobao.api.domain.TaobaokeItem;
import com.taobao.api.domain.TaobaokeItemDetail;
import com.taobao.api.domain.User;
import com.taobao.api.request.ItemsListGetRequest;
import com.taobao.api.request.ShopcatsListGetRequest;
import com.taobao.api.request.TaobaokeItemsDetailGetRequest;
import com.taobao.api.request.UsersGetRequest;
import com.taobao.api.response.ItemsListGetResponse;
import com.taobao.api.response.ShopcatsListGetResponse;
import com.taobao.api.response.TaobaokeItemsDetailGetResponse;
import com.taobao.api.response.UsersGetResponse;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import org.apache.log4j.Logger;

/**
 * Hello world!
 *
 */
public class App 
{
    static Logger logger = Logger.getLogger(App.class);
    
    static final String url = "http://gw.api.taobao.com/router/rest";
    static final String appkey = "12234581";
    static final String appsecret = "d3d4410003ca545aa074c93f3347a42e";
    static final String appkey1 = "12258664";
    static final String appsecret1 = "b23de2be9460659efa37822b8b9ed561";
    static final String appkey2 = "12270366";
    static final String appsecret2 = "f72897f4d8e6de8a55c987283598c505";
    static final String appkey3 = "12272395";
    static final String appsecret3 = "a3a5dc65428ef12892f34bb041c5b162";
    static final String appkey4 = "12272396";
    static final String appsecret4 = "8b879857b71b6e13bb09bd3f896d4aeb";
    static final String appkey5 = "12272619";
    static final String appsecret5 = "668baa3aaae695bd0c48a3a2875927e4";
    static final String appkey6 = "12272617";
    static final String appsecret6 = "61d57a14e6def2118bd324887e9cf516";
    static final String appkey7 = "12272730";
    static final String appsecret7 = "071390eb9e57b0040481453f9159a023";
    static final String appkey8 = "12272729";
    static final String appsecret8 = "d8bdad40a2b209c3dca8e875afd60dcc";
    static final String appkey9 = "12272727";
    static final String appsecret9 = "444b00bb5b7e83aa8b48797ca817a30d";
    static final String appkey10 = "12272877";
    static final String appsecret10 = "ed2841bc70db1c54878a1898abb2bf35";
    static final String appkey11 = "12272875";
    static final String appsecret11 = "ad3b682b27b81f9dd50bca8691e7ba53";
    static final String appkey12 = "12272874";
    static final String appsecret12 = "7ad46841ee8bd37ea4294db3f5486d1c";
    static final String appkey13 = "12274536";
    static final String appsecret13 = "70464659c79e0f190fccc63d8238c8e1";
    static final String appkey14 = "12274537";
    static final String appsecret14 = "660f30958b0a0ddb67b4f5f3815dba6a";
    static final String appkey15 = "12274538";
    static final String appsecret15 = "61e36a4466e10535d24e1203e2feca07";
    static final String appkey16 = "12274539";
    static final String appsecret16 = "25c1919e35baa2067f649961bf89d5cd";
    static final String appkey17 = "12274540";
    static final String appsecret17 = "f54b49143f98c8cf373173d644df46d8";
    static final String appkey18 = "12275351";
    static final String appsecret18 = "dfff0805d02fa52ab63c161785e34aa4";
    static final String appkey19 = "12275350";
    static final String appsecret19 = "dd4c1faf0fa3ec3263dd2b7df0b0ce17";
    static final String appkey20 = "12275348";
    static final String appsecret20 = "a2fa186f55cd9d4079e5591391ba852a";
    static final String appkey21 = "12275347";
    static final String appsecret21 = "cdeba949a99e17ab0fe2cb5bead86085";
    static final String appkey22 = "12275346";
    static final String appsecret22 = "34676db68a1a2df0012bbca4dfa597d9";
    static final String appkey23 = "12276215";
    static final String appsecret23 = "6942e1661adf0a6a1d69d1bcd5da209b";
    static final String appkey24 = "12276217";
    static final String appsecret24 = "37711558af9c8bca9802bacec9eb60f4";
    static final String appkey25 = "12276218";
    static final String appsecret25 = "4e3a357dfd3536fa5e9c6b0fe7cfcc53";
    static final String appkey26 = "12276219";
    static final String appsecret26 = "60b44b1401b284946d0861b0fa26c074";
    static final String appkey27 = "12276220";
    static final String appsecret27 = "8baca3902cf1c33b70e5d6d839c26c7b";
    static final String appkey28 = "12276291";
    static final String appsecret28 = "db628539370d1efe3ea4c2139d18ad31";
    static final String appkey29 = "12276292";
    static final String appsecret29 = "88603994c1655cfbcc640393cfef1495";
    static TaobaoClient[] clients = new TaobaoClient[] { 
        new DefaultTaobaoClient(url, appkey13, appsecret13),
        new DefaultTaobaoClient(url, appkey14, appsecret14),
        new DefaultTaobaoClient(url, appkey15, appsecret15),
        new DefaultTaobaoClient(url, appkey16, appsecret16),
        new DefaultTaobaoClient(url, appkey17, appsecret17),
        new DefaultTaobaoClient(url, appkey23, appsecret23),
        new DefaultTaobaoClient(url, appkey24, appsecret24),
        new DefaultTaobaoClient(url, appkey25, appsecret25),
        new DefaultTaobaoClient(url, appkey26, appsecret26),
        new DefaultTaobaoClient(url, appkey27, appsecret27),
        
        new DefaultTaobaoClient(url, appkey18, appsecret18),
        new DefaultTaobaoClient(url, appkey19, appsecret19),
        new DefaultTaobaoClient(url, appkey20, appsecret20),
        new DefaultTaobaoClient(url, appkey21, appsecret21),
        new DefaultTaobaoClient(url, appkey22, appsecret22),
        new DefaultTaobaoClient(url, appkey28, appsecret28),
        new DefaultTaobaoClient(url, appkey29, appsecret29),
        /**/
       /* new DefaultTaobaoClient(url, appkey, appsecret),
        new DefaultTaobaoClient(url, appkey1, appsecret1),
        new DefaultTaobaoClient(url, appkey2, appsecret2),
        new DefaultTaobaoClient(url, appkey3, appsecret3),
        new DefaultTaobaoClient(url, appkey4, appsecret4),
        new DefaultTaobaoClient(url, appkey5, appsecret5),
        new DefaultTaobaoClient(url, appkey6, appsecret6),
        new DefaultTaobaoClient(url, appkey7, appsecret7),
        new DefaultTaobaoClient(url, appkey8, appsecret8),
        new DefaultTaobaoClient(url, appkey9, appsecret9),
        new DefaultTaobaoClient(url, appkey10, appsecret10),
        new DefaultTaobaoClient(url, appkey11, appsecret11),
        new DefaultTaobaoClient(url, appkey12, appsecret12),
        */
    };
    
    static final String user = "windinn";
    
    public static void main( String[] args ) throws Exception
    {
        //Utils.writeObjectToFile(getShopCats(), "d:/taobao/shopcats.data");
        //crawlShops();
        
        //crawlTaobaokeItems();
        //List<ItemCat> cats = (List<ItemCat>)Utils.readObjectFromFile("d:/taobao/categories.data");
        //for(ItemCat cat : cats) {
        //    System.out.println("" + cat.getParentCid() + "\t" + cat.getCid() + "\t" + cat.getName());
        // }
        //TaobaoUserShopConnector connector = new TaobaoUserShopConnector("d:/taobao/users.data", "d:/taobao/sorted_shops.data", "d:/taobao/categories.data");
        //connector.connect();
        //ItemCategoryCrawler crawler = new ItemCategoryCrawler();
        //crawler.crawl("d:/taobao/categories.data");
        //ItemCategoriesStatistics stat = new ItemCategoriesStatistics("d:/taobao/categories.data");
        //stat.calculate(1801L);
        ItemCategories<ItemCat> cats = new ItemCategories<ItemCat>((List<ItemCat>)Utils.readObjectFromFile("d:/taobao/categories.data"));
        StoreHelper helper = new StoreHelper("f:/taobao", "taobao");
        TaobaoUserAccessor accessor = new TaobaoUserAccessor(helper.getStore());
        StoreHelper helper2 = new StoreHelper("f:/taobao3", "taobao");
        TaobaoUserAccessor accessor2 = new TaobaoUserAccessor(helper2.getStore());
        EntityCursor<TaobaoUserEntity> cursor = null;
        try {
            cursor = accessor.priIndex.entities();
            for(TaobaoUserEntity user : cursor) {
                Set<Long> cids = new HashSet<Long>();
                for(Long cid : user.getCids()) {
                    ItemCat cat = cats.getParent(cid);
                    if(cat != null) {
                        cids.add(cat.getCid());
                    }
                }
                user.getCids().addAll(cids);
                accessor2.putTaobaoUserEntity(user);
            }
        } finally {
            if(cursor != null)
                cursor.close();
        }

        helper.close();
        helper2.close();
}

    private static void getUsers() throws InterruptedException, ClassNotFoundException, IOException, ApiException {
  
        Set<String> nicks = (Set<String>)Utils.readObjectFromFile("d:/taobao/nicks.data");
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:/taobao/users.data"));
        Object[] sortednicks = new TreeSet<String>(nicks).toArray();
        UsersGetRequest req = new UsersGetRequest();
        req.setFields("user_id,uid,nick,sex,seller_credit,location,created,birthday,type,alipay_account,alipay_no,avatar,email,vertical_market");
        for(int i = 0; i < sortednicks.length; i += 40) {
            StringBuilder sb = new StringBuilder();
            for(int j = 0; i + j < sortednicks.length && j < 40; j++) {
                sb.append((String)sortednicks[i + j]).append(",");
            }
            req.setNicks(sb.substring(0, sb.length() - 1));
            UsersGetResponse res = getTaobaoClient().execute(req);
            if(res.isSuccess()) {
                for(User user : res.getUsers())
                    oos.writeObject(user);
            }else {
                logger.warn(res.getMsg());
            }
            if(i % 1000 == 0)
                oos.reset();
            Thread.sleep(100);
        }
        oos.close();
    }

    public static void putTaobaokeItemsIntoDB() throws DatabaseException, FileNotFoundException, IOException {
        StoreHelper helper = new StoreHelper("f:/taobao", "taobao");
        TaobaokeItemAccessor accessor = new TaobaokeItemAccessor(helper.getStore());
    }
    
    public static void extractNicks() throws IOException {
        Set<String> nicks = new HashSet<String>();
        File taobao = new File("d:/taobao");
        File[] files = taobao.listFiles(new FileFilter() {

            public boolean accept(File pathname) {
                String name = pathname.getName();
                return name.startsWith("taobaokeitems") && name.endsWith(".data");
            }
        });
        for(File file : files) {
            logger.info(file.getName());
            extractNicks(file, nicks);
        }
        Utils.writeObjectToFile(nicks, "d:/taobao/nicks.data");
    }

    private static void extractNicks(File file, Set<String> nicks) throws IOException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        try {
            while(true) {
                Object obj = ois.readObject();
                if(obj == null)
                    break;
                TaobaokeItem item = (TaobaokeItem)obj;
                nicks.add(item.getNick());
            }
        } catch (IOException ex) {
            logger.error(ex);
        } catch (ClassNotFoundException ex) {
            logger.error(ex);
        }
        ois.close();
    }
    
    public static void getDetail(List<TaobaokeItem> items) throws ApiException {
        ItemsListGetRequest req = new ItemsListGetRequest();
        req.setFields("nick, cid, product_id, location");
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < 20; i++) 
            sb.append(items.get(i).getNumIid()).append(",");
        req.setNumIids(sb.substring(0, sb.length() - 1));
        //req.setNick(user);
        ItemsListGetResponse res = getTaobaoClient().execute(req);
        if(res.isSuccess()) {
            for(Item item : res.getItems()) {
                logger.info(item.getLocation());
                logger.info(item.getProductId());
                logger.info(item.getNick()); 
            }
        }else 
            logger.warn(res.getMsg());
    }
  
    private static void crawlTaobaokeItems() throws IOException, InterruptedException, ClassNotFoundException {
        List<ItemCat> cats = (List<ItemCat>)Utils.readObjectFromFile("d:/taobao/categories.data");
        TaobaokeItemCrawler crawler = new TaobaokeItemCrawler(cats, 100);
        crawler.crawl("d:/taobao/taobaokeitems.data");
    }

    private static void crawlShops() throws IOException, InterruptedException, ClassNotFoundException {
        List<ShopCat> cats = (List<ShopCat>)Utils.readObjectFromFile("d:/taobao/shopcats.data");
        ShopCrawler crawler = new ShopCrawler(cats, 1000);
        crawler.crawl("d:/taobao/taobaokeshops.data");
    }
    
    static int index = 0;
    public static TaobaoClient getTaobaoClient() {
        index++;
        if(index >= clients.length) index = 0;
        return clients[index];
    }
    
    private static List<ShopCat> getShopCats() throws ApiException {
        TaobaoClient client = getTaobaoClient();
        ShopcatsListGetRequest req = new ShopcatsListGetRequest();
        ShopcatsListGetResponse res = client.execute(req);
        if(!res.isSuccess())
            logger.warn(res.getMsg());
        return res.getShopCats();
    }
}
